package com.fingard.dsp.bank.directbank.bocom01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.EBankSet;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;
import org.springframework.stereotype.Component;

import java.util.Date;
@Component("BOCOM01SinglePay")
public class BOCOM01SinglePay extends BOCOM01Base implements ISinglePay {

    /**
     * 单笔付款指令提交
     * @param p_req1908
     * @return
     * @throws Exception
     */
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail tmpRetDetail;
        // 单笔总共分为四个模式，行内调拨（同行对公） 190801 ，210202
        // 对外转账 。210224 超级网银，小于5W接口，跨行对私
        // 对外转账，210201 跨行对私，跨行对公，同行对公
        // 同行对私转账， 330002
        boolean tmpIsForceInnerPay = getBankConfig().getValueAsBool("AutoSelectSameBankInner");
        // 同行对私，并且行内调拨的接口开启，就直接走210202
        if (p_req1908.reqHeader.commandCode.equals("190801") && p_req1908.singleDetail.sameBnk.equals("1") && tmpIsForceInnerPay) {
            // 走企业内转账
            tmpRetDetail = d_1908_210202(p_req1908);
        } else if (p_req1908.reqHeader.commandCode.equals("190804B")) {
            // && p_req1908.singleDetail.isPrivate.equals("1")
            // && p_req1908.singleDetail.sameBnk.equals("1")
            // 走单笔同行对私支付330002
            tmpRetDetail = d_1908_330002(p_req1908);
        } else if (p_req1908.reqHeader.commandCode.equals("190803")) {
            // 代发工资、报销差旅费支持非交行卡的批量接口330008
            tmpRetDetail = d_1908_330008(p_req1908);
        }else {
            // 走对外转账
            //boolean IsEBank = getBankConfig().getValueAsBool("IsEBank");
            EBankSet eBankSet = getBankConfig().getEBankSet(reqHeader.commandCode, reqHeader.transType);
            if ("0".equals(p_req1908.singleDetail.sameBnk) && eBankSet != null && eBankSet.isEnable && eBankSet.amtLimit.compareTo(p_req1908.singleDetail.getPayAmount()) >= 0) {
                // 两个条件同时满足的情况下走超级网银210224
                tmpRetDetail = d_1908_210224(p_req1908);
            } else {
                // 否则走对外支付 210201
                tmpRetDetail = d_1908_210201(p_req1908);
            }

        }
        if (tmpRetDetail.respCode.equals("-1") && StringHelper.isNullOrEmpty(tmpRetDetail.reqSeqID)) {
            tmpRetDetail.reqSeqID = p_req1908.singleDetail.askNumber;
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;

    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    /**
     * 单笔富矿状态查询
     * @param p_req1944
     * @return
     * @throws Exception
     */
    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        if ("190804B".equals(p_req1944.reqHeader.commandCode)) {
            return d_1944_QrySglPriPay(p_req1944);
        } else if ("190803".equals(p_req1944.reqHeader.commandCode)) {
            return d_1944_QrySgl_310207(p_req1944);
        }

        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail retDetail = new RetAtsSglDetail(p_req1944);
        tr_code = "310204";
        XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
        writeCommonHeader(xmlWriter);// 写入通用的头部节点
        xmlWriter.writeStartElement("body");
        xmlWriter.writeElementString("query_flag", "1");
        xmlWriter.writeElementString("ogl_serial_no", p_req1944.askNumber);
        xmlWriter.writeEndDocument();
        //String a = xmlWriter.toXmlString();
        try {
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());// 发送银行
            //	String xmlString = "<ap>" + "<head>" + "<tr_code>310204</tr_code>" + "<corp_no>8000360459</corp_no>" + "<req_no>1648086268</req_no>" + "<serial_no> </serial_no>" + "<ans_no> </ans_no>" + "<next_no> </next_no>" + "<tr_acdt>20160609</tr_acdt>" + "<tr_time>165035</tr_time>" + "<ans_code>0</ans_code>" + "<ans_info> </ans_info>" + "	<particular_code>0000</particular_code>" + "<particular_info> </particular_info>" + "<atom_tr_count>1</atom_tr_count>" + "<reserved></reserved>" + "</head>" + "<body>" + "<ogl_serial_no>2011000420191201163851999627</ogl_serial_no>" + "<stat>1</stat>" + "<err_msg>记账成功</err_msg>" + "<pay_acno>110060149018170160734</pay_acno>" + "<pay_bank_no>01110100999</pay_bank_no>" + "<rcv_acno>387058654873</rcv_acno>" + "<rcv_bank_no>104331051296</rcv_bank_no>"
            //	+ "<amt>10.00</amt>" + "<cert_no>26082800427653</cert_no>" + "</body>" + "</ap>";
            //String[] tmpStrRet = new String[] { "", xmlString };
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String particular_code = tmpBkReader.getFirstText("/ap/head/particular_code");
                    String stat = tmpBkReader.getFirstText("/ap/body/stat");
                    String err_msg = tmpBkReader.getFirstText("/ap/body/err_msg");
                    retDetail.transState = "Unknow";// 默认为支付中
                    retDetail.respCode = "0";
                    if (stat.length() > 0) {
                        retDetail.transState = particular_code + "+" + stat;
                        if (err_msg.length() > 0) {
                            retDetail.respInfo = err_msg;
                            retDetail.payInfoCode = err_msg;
                        }
                        retDetail.transAmt = tmpBkReader.getFirstText("/ap/body/amt");
                    }

                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpSbMsg.toString();
                }
            } else {

                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1].toString();
            }

        } catch (Exception e) {
            retDetail.respCode = "-1";
            retDetail.respInfo = e.getMessage();
            e.printStackTrace();
        }

        retAts.writeRetDetail(retDetail);
        return retAts;
    }

    public RetAtsSingle d_1944_QrySglPriPay(ReqQrySingle p_req1944) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail retDetail = new RetAtsSglDetail(p_req1944);

        tr_code = "310200";
        XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
        writeCommonHeader(xmlWriter);// 写入ICBC02通用的头部节点
        xmlWriter.writeStartElement("body");
        xmlWriter.writeElementString("query_flag", "1");
        xmlWriter.writeElementString("ogl_serial_no", p_req1944.askNumber);
        xmlWriter.writeEndDocument();

        try {
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/ap/head/particular_code");
                    String tmpRetMsg = tmpBkReader.getFirstText("/ap/head/particular_info");
                    if (!StringHelper.isNullOrEmpty(tmpRetCode)) {
                        if (isActiveRetCode(tmpRetCode)) {
                            String serial_record = tmpBkReader.getFirstText("/ap/body/serial_record");
                            int FieldNum = Integer.parseInt(tmpBkReader.getFirstText("/ap/body/field_num"));
                            int RecordNum = Integer.parseInt(tmpBkReader.getFirstText("/ap/body/record_num"));
                            java.util.List<String[]> listStrings = DealMFSText(serial_record, '|', FieldNum, RecordNum);
                            retDetail.respCode = "0";
                            if (listStrings.size() > 0) {
                                listStrings.remove(0);
                                for (String[] strings : listStrings) {
                                    // （C20）| 0
                                    // 所属月份C(6)| 1
                                    // 付款账号（C21）|2
                                    // 会计核心日期（C8）|3
                                    // 批次流水号（C8）|4
                                    // 序号（C8）|5
                                    // 付款户名（C60）| 6
                                    // 付款币种（C3）|7
                                    // 业务类型（C30）|8
                                    // 付款金额（N14，2）|9
                                    // 收款卡号(C25)| 10
                                    // 收款户名(C60)|11
                                    // 状态(C1)|12
                                    // 错误原因(C30)| 13
                                    // 业务编号（20 14
                                    retDetail.transAmt = strings[9];
                                    retDetail.transState = tmpRetCode + "+" + strings[12];
                                    retDetail.payInfoCode = strings[13];
                                    retDetail.respInfo = strings[13];
                                    retAts.writeRetDetail(retDetail);
                                    return retAts;

                                }
                            } else {
                                retDetail.respCode = "-1";
                                retDetail.respInfo = tmpRetMsg;
                            }

                        } else {
                            retDetail.respCode = "-1";
                            retDetail.respInfo = tmpRetMsg;
                        }
                    } else {
                        retDetail.respCode = "-1";
                        retDetail.respInfo = "节点/ap/head/particular_code为空";
                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1];
            }

        } catch (Exception e) {
            retDetail.respCode = "-1";
            retDetail.respInfo = e.getMessage();
            e.printStackTrace();
        }
        if ("-1".equals(retDetail.respCode) && StringHelper.isNullOrEmpty(retDetail.reqSeqID)) {
            retDetail.reqSeqID = p_req1944.askNumber;
        }

        retAts.writeRetDetail(retDetail);
        return retAts;
    }

    public RetAtsSingle d_1944_QrySgl_310207(ReqQrySingle p_req1944) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail retDetail = new RetAtsSglDetail(p_req1944);

        try {
            tr_code = "310207";
            XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
            writeCommonHeader(xmlWriter);// 写入ICBC02通用的头部节点
            xmlWriter.writeStartElement("body");
            xmlWriter.writeElementString("ogl_serial_no", p_req1944.askNumber);
            xmlWriter.writeElementString("query_flag", "1");
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/ap/head/particular_code");
                    String tmpRetMsg = tmpBkReader.getFirstText("/ap/head/particular_info");
                    if (!StringHelper.isNullOrEmpty(tmpRetCode)) {
                        if (isActiveRetCode(tmpRetCode)) {
                            String serial_record = tmpBkReader.getFirstText("/ap/body/serial_record");
                            int FieldNum = Integer.parseInt(tmpBkReader.getFirstText("/ap/body/field_num"));
                            int RecordNum = Integer.parseInt(tmpBkReader.getFirstText("/ap/body/record_num"));
                            java.util.List<String[]> listStrings = DealMFSText(serial_record, '|', FieldNum, RecordNum);
                            retDetail.respCode = "0";
                            if (listStrings.size() > 0) {
                                listStrings.remove(0);
                                for (String[] strings : listStrings) {
                                    retDetail.transAmt = strings[9];
                                    if (strings.length > 12) {
                                        retDetail.transState = tmpRetCode + "+" + strings[12];
                                    }
                                    if (strings.length > 13) {
                                        retDetail.payInfoCode = strings[13];
                                        retDetail.respInfo = strings[13];
                                    }
                                }
                            } else {
                                retDetail.respCode = "-1";
                                retDetail.respInfo = tmpRetMsg;
                            }

                        } else {
                            retDetail.respCode = "-1";
                            retDetail.respInfo = tmpRetMsg;
                        }
                    } else {
                        retDetail.respCode = "-1";
                        retDetail.respInfo = "节点/ap/head/particular_code为空";
                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retDetail.respCode = "-1";
            retDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetDetail(retDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }

    // 单笔总共分为四个模式，行内调拨（同行对公） 190801 ，210202
    private RetAtsSglDetail d_1908_210202(ReqSingle p_req1908) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail(p_req1908);

        try {
            tr_code = "210202";
            XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
            // 写入通用的头部节点
            writeCommonHeader(xmlWriter);
            XElement tmpXElement = new XElement("body", "");
            tmpXElement.addChild(new XElement("pay_acname", p_req1908.singleDetail.payName));
            tmpXElement.addChild(new XElement("pay_acno", p_req1908.singleDetail.payAct));
            tmpXElement.addChild(new XElement("rcv_acname", p_req1908.singleDetail.recName));
            tmpXElement.addChild(new XElement("rcv_acno", p_req1908.singleDetail.recAct));
            tmpXElement.addChild(new XElement("rcv_bank_name", p_req1908.singleDetail.recBankName));
            tmpXElement.addChild(new XElement("amt", p_req1908.singleDetail.payAmount));
            tmpXElement.addChild(new XElement("tr_time", Format.Time8Format.format(new Date())));
            tmpXElement.addChild(new XElement("tr_acdt", Format.Date8Format.format(new Date())));
            // 币种
            tmpXElement.addChild(new XElement("cur_code", this.convertCurToBank(p_req1908.singleDetail.recCur)));
            if ("0".equalsIgnoreCase(p_req1908.singleDetail.primaryAct)) {
                tmpXElement.addChild(new XElement("pay_flag", "1"));
            } else {
                tmpXElement.addChild(new XElement("pay_flag", "0"));
            }
            tmpXElement.addChild(new XElement("cert_no", p_req1908.singleDetail.askNumber));
            // 对账码组装，以后在完善。目前还没有公共方法。
            //p_req1908.singleDetail.abstractATS + "|" +
            tmpXElement.addChild(new XElement("summary", p_req1908.singleDetail.purpose));
            nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, "GBK");
            xmlWriter.writeXElement(tmpXElement);
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());// 发送银行
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                // 解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    retDetail.respCode = "0";
                    if (!tmpBkReader.getFirstText("/ap/head/particular_code").equals("")) {
                        retDetail.transState = tmpBkReader.getFirstText("/ap/head/particular_code");
                        retDetail.payInfoCode = tmpBkReader.getFirstText("/ap/head/particular_code");
                        retDetail.respInfo = tmpBkReader.getFirstText("/ap/head/particular_info");
                        String ans_info = tmpBkReader.getFirstText("/ap/head/ans_info");
                        if (StringHelper.hasAnyChar(ans_info)) {
                            retDetail.respInfo = ans_info + "+" + retDetail.respInfo;
                            if ("9003".equals(retDetail.transState)) {
                                retDetail.transState = retDetail.transState + "+" + ans_info;
                                retDetail.payInfoCode = retDetail.transState;
                            }
                        }
                    } else if (!tmpBkReader.getFirstText("/ap/body/tranStatus").equals("")) {
                        retDetail.respInfo = tmpBkReader.getFirstText("/ap/head/particular_info");
                        retDetail.transState = tmpBkReader.getFirstText("/ap/head/tr_code") + "+" + tmpBkReader.getFirstText("/ap/body/tranStatus");
                        retDetail.payInfoCode = tmpBkReader.getFirstText("/ap/body/tranStatus");
                    }

                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            retDetail.respCode = "-1";
            retDetail.respInfo = e.toString();
        }
        return retDetail;
    }

    // 对外转账 。210224 超级网银，小于5W接口，跨行对私
    private RetAtsSglDetail d_1908_210224(ReqSingle p_req1908) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail(p_req1908);

        try {
            tr_code = "210224";
            XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
            // 写入通用的头部节点
            writeCommonHeader(xmlWriter);
            XElement tmpXElement = new XElement("body", "");
            tmpXElement.addChild(new XElement("pay_acno", p_req1908.singleDetail.payAct));
            tmpXElement.addChild(new XElement("pay_acname", p_req1908.singleDetail.payName));
            tmpXElement.addChild(new XElement("rcv_acno", p_req1908.singleDetail.recAct));
            tmpXElement.addChild(new XElement("rcv_acname", p_req1908.singleDetail.recName));
            tmpXElement.addChild(new XElement("partyid", p_req1908.singleDetail.cnapsCode));// 联行号
            tmpXElement.addChild(new XElement("amt", p_req1908.singleDetail.payAmount));
            tmpXElement.addChild(new XElement("cert_no", p_req1908.singleDetail.askNumber));
            tmpXElement.addChild(new XElement("purpose", p_req1908.singleDetail.purpose));
            tmpXElement.addChild(new XElement("summary", p_req1908.singleDetail.purpose));
            nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, "GBK");
            xmlWriter.writeXElement(tmpXElement);
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());// 发送银行
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                // 解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    retDetail.respCode = "0";

                    if (!tmpBkReader.getFirstText("/ap/head/particular_code").equals("")) {
                        retDetail.transState = tmpBkReader.getFirstText("/ap/head/particular_code");
                        retDetail.payInfoCode = tmpBkReader.getFirstText("/ap/head/particular_code");
                        retDetail.respInfo = tmpBkReader.getFirstText("/ap/head/particular_info");
                        String ans_info = tmpBkReader.getFirstText("/ap/head/ans_info");
                        if (StringHelper.hasAnyChar(ans_info)) {
                            retDetail.respInfo = ans_info + "+" + retDetail.respInfo;
                            if ("9003".equals(retDetail.transState)) {
                                retDetail.transState = retDetail.transState + "+" + ans_info;
                                retDetail.payInfoCode = retDetail.transState;
                            }
                        }
                    } else if (!tmpBkReader.getFirstText("/ap/body/tranStatus").equals("")) {
                        retDetail.respInfo = tmpBkReader.getFirstText("/ap/head/particular_info");
                        retDetail.transState = tmpBkReader.getFirstText("/ap/head/tr_code") + "+" + tmpBkReader.getFirstText("/ap/body/tranStatus");
                        retDetail.payInfoCode = tmpBkReader.getFirstText("/ap/body/tranStatus");

                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1].toString();
            }
        } catch (Exception e) {
            retDetail.respCode = "-1";
            retDetail.respInfo = e.toString();
        }
        return retDetail;
    }

    // 对外转账，210201 跨行对私，跨行对公，同行对公
    private RetAtsSglDetail d_1908_210201(ReqSingle p_req1908) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail(p_req1908);

        try {
            tr_code = "210201";
            req_no = p_req1908.reqHeader.reqSeqID;
            XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
            // 写入通用的头部节点
            writeCommonHeader(xmlWriter);
            XElement tmpXElement = new XElement("body", "");
            tmpXElement.addChild(new XElement("pay_acname", p_req1908.singleDetail.payName));
            tmpXElement.addChild(new XElement("pay_acno", p_req1908.singleDetail.payAct));
            tmpXElement.addChild(new XElement("rcv_acname", p_req1908.singleDetail.recName));
            tmpXElement.addChild(new XElement("rcv_acno", p_req1908.singleDetail.recAct));
            tmpXElement.addChild(new XElement("rcv_bank_name", p_req1908.singleDetail.cnapsName));
            tmpXElement.addChild(new XElement("amt", p_req1908.singleDetail.payAmount));
            // xmlWriter.writeElementString("tr_time",
            // Format.Time8Format.format(new
            // Date()));
            // xmlWriter.writeElementString("tr_acdt",
            // Format.Date8Format.format(new
            // Date()));
            // 币种
            tmpXElement.addChild(new XElement("cur_code", this.convertCurToBank(p_req1908.singleDetail.recCur)));
            tmpXElement.addChild(new XElement("cert_no", p_req1908.singleDetail.askNumber));
            // 对账码组装，以后在完善。目前还没有公共方法。
            //p_req1908.singleDetail.abstractATS + "|" +
            tmpXElement.addChild(new XElement("summary", p_req1908.singleDetail.purpose));
            if (p_req1908.singleDetail.sameBnk.equals("1")) {
                tmpXElement.addChild(new XElement("bank_flag", "0"));
                if (p_req1908.singleDetail.sameCity.equals("1")) {
                    tmpXElement.addChild(new XElement("area_flag", "0"));
                } else {
                    tmpXElement.addChild(new XElement("area_flag", "1"));
                }
            } else {
                tmpXElement.addChild(new XElement("bank_flag", "1"));
                tmpXElement.addChild(new XElement("area_flag", "1"));

            }

            // xmlWriter.writeElementString("rcv_exg_code", "");
            tmpXElement.addChild(new XElement("rcv_bank_no", p_req1908.singleDetail.cnapsCode));// 联行号
            nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, "GBK");
            xmlWriter.writeXElement(tmpXElement);
            xmlWriter.writeEndDocument();
            // String a = xmlWriter.toXmlString();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());// 发送银行
            // String xmlString =
            // "<ap><head><tr_code>210201</tr_code><corp_no>8000360459</corp_no><req_no>70000000160000000090</req_no><serial_no>2011000420191201163851995737</serial_no><ans_no> </ans_no><next_no> </next_no><tr_acdt>20160604</tr_acdt><tr_time>100439</tr_time><ans_code>0</ans_code><ans_info> </ans_info><particular_code>0000</particular_code><particular_info> </particular_info><atom_tr_count>1</atom_tr_count><reserved></reserved></head></ap>";
            // String[] tmpStrRet = new String[] { "", xmlString };
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                // 解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    retDetail.respCode = "0";

                    if (!tmpBkReader.getFirstText("/ap/head/particular_code").equals("")) {
                        retDetail.transState = tmpBkReader.getFirstText("/ap/head/particular_code");
                        retDetail.payInfoCode = tmpBkReader.getFirstText("/ap/head/particular_code");
                        retDetail.respInfo = tmpBkReader.getFirstText("/ap/head/particular_info");
                        String ans_info = tmpBkReader.getFirstText("/ap/head/ans_info");
                        if (StringHelper.hasAnyChar(ans_info)) {
                            retDetail.respInfo = ans_info + "+" + retDetail.respInfo;
                            if ("9003".equals(retDetail.transState)) {
                                retDetail.transState = retDetail.transState + "+" + ans_info;
                                retDetail.payInfoCode = retDetail.transState;
                            }
                        }
                    } else if (!tmpBkReader.getFirstText("/ap/body/tranStatus").equals("")) {
                        retDetail.respInfo = tmpBkReader.getFirstText("/ap/head/particular_info");
                        retDetail.transState = tmpBkReader.getFirstText("/ap/head/tr_code") + "+" + tmpBkReader.getFirstText("/ap/body/tranStatus");
                        retDetail.payInfoCode = tmpBkReader.getFirstText("/ap/body/tranStatus");

                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1].toString();
            }
        } catch (Exception e) {
            retDetail.respCode = "-1";
            retDetail.respInfo = e.toString();
        }
        return retDetail;
    }

    // 同行对私转账， 330002
    private RetAtsSglDetail d_1908_330002(ReqSingle p_req1908) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail(p_req1908);

        try {
            tr_code = "330002";
            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter);
            XElement tmpXElement = new XElement("body", "");
            tmpXElement.addChild(new XElement("cert_no", p_req1908.singleDetail.askNumber));
            req_no = p_req1908.singleDetail.askNumber;
            tmpXElement.addChild(new XElement("pay_acno", p_req1908.singleDetail.payAct));
            tmpXElement.addChild(new XElement("type", getBankConfig().getMapValueByKey("type_Mapping", p_req1908.singleDetail.purpose)));
            tmpXElement.addChild(new XElement("sum", "1"));
            tmpXElement.addChild(new XElement("sum_amt", p_req1908.singleDetail.payAmount));
            tmpXElement.addChild(new XElement("pay_month", Format.Date6YearMonth.format(new Date())));
            tmpXElement.addChild(new XElement("summary", p_req1908.singleDetail.abstractATS));
            tmpXElement.addChild(new XElement("busi_no", getBankConfig().getMapValueByKey("busi_no_Mapping", p_req1908.singleDetail.payAct)));
            tmpXElement.addChild(new XElement("mailflg", getBankConfig().getValueAsString("mailflg")));// 传票汇总标识。

            XElement tmpTranXElement = new XElement("tran", "");
            XElement tmpRcdXElement = new XElement("rcd", "");
            tmpRcdXElement.addChild(new XElement("card_no", p_req1908.singleDetail.recAct));
            tmpRcdXElement.addChild(new XElement("acname", p_req1908.singleDetail.recName));
            if (p_req1908.singleDetail.recAct.length() == 17 || p_req1908.singleDetail.recAct.length() == 19 || p_req1908.singleDetail.recAct.length() == 16) {
                tmpRcdXElement.addChild(new XElement("card_flag", "0"));
            } else {
                tmpRcdXElement.addChild(new XElement("card_flag", "1"));
            }
            tmpRcdXElement.addChild(new XElement("amt", p_req1908.singleDetail.payAmount));
            tmpRcdXElement.addChild(new XElement("busino", p_req1908.singleDetail.askNumber));
            tmpTranXElement.addChild(tmpRcdXElement);
            tmpXElement.addChild(tmpTranXElement);
            nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());
            xmlWriter.writeXElement(tmpXElement);

            xmlWriter.writeEndDocument();// 写入报文完成

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                // 解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {

                    String tmpStrParCode = tmpBkReader.getFirstText("/ap/head/particular_code");
                    String tmpStrParInfo = tmpBkReader.getFirstText("/ap/head/particular_info");

                    if (!StringHelper.isNullOrEmpty(tmpStrParCode)) {
                        retDetail.respCode = "0";
                        retDetail.transState = tmpStrParCode;
                        retDetail.respInfo = tmpStrParInfo;
                        retDetail.payInfoCode = tmpStrParCode;
                    } else {
                        retDetail.respCode = "-1";
                        retDetail.respInfo = "/ap/head/particular_code节点为空";
                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1];
            }

        } catch (Exception e) {
            retDetail.respCode = "-1";
            retDetail.respInfo = e.toString();
            e.printStackTrace();
        }
        return retDetail;
    }
    private RetAtsSglDetail d_1908_330008(ReqSingle p_req1908) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail(p_req1908);

        try {
            tr_code = "330008";
            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter);
            XElement tmpXElement = new XElement("body", "");
            tmpXElement.addChild(new XElement("cert_no", p_req1908.singleDetail.askNumber));
            req_no = p_req1908.singleDetail.askNumber;
            tmpXElement.addChild(new XElement("pay_acno", p_req1908.singleDetail.payAct));
            tmpXElement.addChild(new XElement("type", getBankConfig().getMapValueByKey("type_Mapping", p_req1908.singleDetail.purpose)));
            tmpXElement.addChild(new XElement("sum", "1"));
            tmpXElement.addChild(new XElement("sum_amt", p_req1908.singleDetail.payAmount));
            tmpXElement.addChild(new XElement("pay_month", Format.Date6YearMonth.format(new Date())));
            tmpXElement.addChild(new XElement("summary", p_req1908.singleDetail.abstractATS));
            tmpXElement.addChild(new XElement("busi_no", getBankConfig().getMapValueByKey("busi_no_Mapping", p_req1908.singleDetail.payAct)));
            tmpXElement.addChild(new XElement("mailflg", getBankConfig().getValueAsString("mailflg")));// 传票汇总标识。

            XElement tmpTranXElement = new XElement("tran", "");
            XElement tmpRcdXElement = new XElement("rcd", "");
            tmpRcdXElement.addChild(new XElement("card_no", p_req1908.singleDetail.recAct));
            tmpRcdXElement.addChild(new XElement("acname", p_req1908.singleDetail.recName));
            if ("1".equalsIgnoreCase(p_req1908.singleDetail.sameBnk)) {
                tmpRcdXElement.addChild(new XElement("card_flag", "A"));
            } else {
                tmpRcdXElement.addChild(new XElement("card_flag", "B"));
            }
            tmpRcdXElement.addChild(new XElement("amt", p_req1908.singleDetail.payAmount));
            tmpRcdXElement.addChild(new XElement("busino", p_req1908.singleDetail.askNumber));
            tmpTranXElement.addChild(tmpRcdXElement);
            tmpXElement.addChild(tmpTranXElement);
            nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());
            xmlWriter.writeXElement(tmpXElement);

            xmlWriter.writeEndDocument();// 写入报文完成

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                // 解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {

                    String tmpStrParCode = tmpBkReader.getFirstText("/ap/head/particular_code");
                    String tmpStrParInfo = tmpBkReader.getFirstText("/ap/head/particular_info");

                    if (!StringHelper.isNullOrEmpty(tmpStrParCode)) {
                        retDetail.respCode = "0";
                        retDetail.transState = tmpStrParCode;
                        retDetail.respInfo = tmpStrParInfo;
                        retDetail.payInfoCode = tmpStrParCode;
                    } else {
                        retDetail.respCode = "-1";
                        retDetail.respInfo = "/ap/head/particular_code节点为空";
                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1];
            }

        } catch (Exception e) {
            retDetail.respCode = "-1";
            retDetail.respInfo = e.toString();
            e.printStackTrace();
        }
        return retDetail;
    }
}
